﻿// <copyright file="FixedHostNameServiceHostFactory.cs" company="SharpSTS">
// Copyright (c) 2007, 2008 All Right Reserved, http://sharpsts.com/
//
// This source is subject to the Microsoft Permissive License.
// Please see the License.txt file for more information.
// All other rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// </copyright>
// <author>Barry Dorrans</author>
// <email>barryd@idunno.org</email>
// <date>2008-06-14</date>
// <summary>Provides a custom service host which fixes up host addresses.</summary>

namespace SharpSTS.Web
{
    using System;
    using System.Collections.Generic;
    using System.ServiceModel;
    using System.ServiceModel.Activation;

    using Configuration;

    /// <summary>
    /// Factory that provides instances of ServiceHost in managed hosting environments where the host instance is 
    /// created dynamically in response to incoming messages and where the listening host addresses are specified
    /// in custom configuration file entries.
    /// </summary>
    internal class FixedHostNameServiceHostFactory : ServiceHostFactory
    {
        /// <summary>
        /// Fixes the base addresses passed in to use the web.config specified base addresses.
        /// </summary>
        /// <param name="baseAddresses">The base addresses.</param>
        /// <returns>An array of fixed <see cref="Uri"/> addresses.</returns>
        protected static Uri[] FixedHostNames(IEnumerable<Uri> baseAddresses)
        {
            List<Uri> fixedUpAddresses = new List<Uri>();

            foreach (Uri uri in baseAddresses)
            {
                string originalHost = uri.Host;
                string host = uri.Host;
                if (uri.Scheme == "http" || uri.Scheme == "https")
                {
                    switch (uri.Scheme)
                    {
                        case "http":
                            if (uri.Host.ToUpperInvariant() !=
                                STSSettings.Settings.BaseSTSAddress.Host.ToUpperInvariant())
                            {
                                host = STSSettings.Settings.BaseSTSAddress.Host;
                            }

                            break;

                        case "https":
                            if (uri.Host.ToUpperInvariant() !=
                                STSSettings.Settings.BaseSTSAddress.Host.ToUpperInvariant())
                            {
                                host = STSSettings.Settings.BaseMetadataExchangeAddress.Host;
                            }

                            break;
                    }

                    Uri fixedAddress = uri;
                    if (host != originalHost)
                    {
                        fixedAddress = new Uri(uri.ToString().Replace(originalHost, host));
                    }

                    if (!fixedUpAddresses.Contains(fixedAddress))
                    {
                        fixedUpAddresses.Add(fixedAddress);
                    }
                }
                else
                {
                    if (!fixedUpAddresses.Contains(uri))
                    {
                        fixedUpAddresses.Add(uri);
                    }
                }
            }
            
            return fixedUpAddresses.ToArray();
        }

        /// <summary>
        /// Creates a <see cref="T:System.ServiceModel.ServiceHost"/> for a specified type of service with a specific base address.
        /// </summary>
        /// <param name="serviceType">Specifies the type of service to host.</param>
        /// <param name="baseAddresses">The <see cref="T:System.Array"/> of type <see cref="T:System.Uri"/> that contains the base addresses for the service hosted.</param>
        /// <returns>
        /// A <see cref="T:System.ServiceModel.ServiceHost"/> for the type of service specified with a specific base address.
        /// </returns>
        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
            baseAddresses = FixedHostNames(baseAddresses);
            ServiceHost serviceHost = new ServiceHost(serviceType, baseAddresses);
            return serviceHost;
        }
    }
}
